package com.kommii.mongodb_utils;

import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.List;

//import org.identity.log.LogPrn;

import org.kommii.common_utils.RandomInt_generator;
import com.mongodb.DB;
import com.mongodb.DBCollection;
import com.mongodb.Mongo;
import com.mongodb.MongoOptions;
import com.mongodb.ServerAddress;

public class Mongodb_ConnPools {
	
	//private static LogPrn log = new LogPrn(Mongodb_ConnPools.class);
	protected static String dbServerIP;
    protected static int dbServerPort;
    protected static String dbName;
    protected static String dbUserName;
    protected static String dbPassword;
    protected static String connectionDescriptor=null;
    private static int connectionsPerHost;
    private static int threadsAllowedToBlockForConnectionMultiplier;
    private static int connectTimeout;
    private static boolean autoConnectRetry;
    
    //private static DB db;
    //private static DB db2;
    private static final int size = 10;
    private static DB[] DBArry;
	static {
		dbServerIP=MongoDb_Config.getDbServerIP();
		dbServerPort=MongoDb_Config.getDbServerPort();
		dbName=MongoDb_Config.getDbName();
		dbUserName=MongoDb_Config.getDbUserName();
		dbPassword=MongoDb_Config.getDbPassword();
		connectionDescriptor=MongoDb_Config.getConnectionDescriptor();
		connectionsPerHost=MongoDb_Config.getConnectionsPerHost();
		threadsAllowedToBlockForConnectionMultiplier=MongoDb_Config.getThreadsAllowedToBlockForConnectionMultiplier();
		connectTimeout=MongoDb_Config.getConnectTimeout();
		autoConnectRetry=MongoDb_Config.isAutoConnectRetry();
		
		 try {
	            Mongo mongo;
	            if (connectionDescriptor != null) {
	                String[] hosts = connectionDescriptor.split(",");
	                List<ServerAddress> addr = new ArrayList<ServerAddress>();
	                for (String host: hosts) {
	                    String[] hostPortPair = host.split(":");
	                    int port = 27017;
	                    if (hostPortPair.length > 1) {
	                        try {
	                            port = Integer.parseInt(hostPortPair[1]);
	                        } catch (NumberFormatException e) {
	                            e.printStackTrace();
	                        }
	                    }
	                    addr.add(new ServerAddress(hostPortPair[0], port));
	                }
	                mongo = new Mongo(addr); 
	            } else {
	                mongo = new Mongo(dbServerIP, dbServerPort);
	            }
	            //连接池参数
	          	DBArry = new DB[size];
	         for (int i = 0; i < size; i++) {
				Mongo m = new Mongo(dbServerIP, dbServerPort);
				MongoOptions opt = m.getMongoOptions();
				opt.setMaxWaitTime(30000);
				opt.setConnectionsPerHost(connectionsPerHost);
				opt.setThreadsAllowedToBlockForConnectionMultiplier(threadsAllowedToBlockForConnectionMultiplier);
				opt.setAutoConnectRetry(autoConnectRetry);
				opt.setConnectTimeout(connectTimeout);
				MongoOptions opt2 = m.getMongoOptions();
				DBArry[i] = m.getDB(dbName);
			}
	         System.out.println("init mongodb conn pool "+size*10);
	        } catch (UnknownHostException e) {
	            throw new RuntimeException(e);
	        }
	}
	
	/**
	 * 获取mongodb数据库中的某个集合
	 * @param coll_name
	 * @return
	 */
	public static DBCollection getMongoDB_Coll(String coll_name){
		
		int i = RandomInt_generator.generatorInt(size);
		if(null!=dbUserName && null!=dbPassword && !"".equals(dbPassword) && !"".equals(dbUserName)){
			boolean b = DBArry[i].authenticate(dbUserName, dbPassword.toCharArray());
		}
		return DBArry[i].getCollection(coll_name);
	}
	
	public static void commitMongoDB_Coll(DBCollection coll){
		coll.getDB().requestDone();
	}
	
	/**
	 * 获取mongodb数据库的连接
	 * @return
	 */
	public static DB getMongoDB_Conn(){
		return DBArry[0];
	}
	
}
